package com.qilin.hscs.hm.mqtt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.qilin.hscs.hm.convert.DTO2DOConvert;
import com.qilin.hscs.hm.dao.MuticontrollerRepository;
import com.qilin.hscs.hm.dto.MuticontrollerDto;
import com.qilin.hscs.hm.enums.CommonException;
import com.qilin.hscs.hm.enums.ResultEnum;
import com.qilin.hscs.hm.mqtt.service.MutiControllerService;
import com.qilin.hscs.hm.mqtt.service.common.ServiceCommon;
import com.qilin.hscs.hm.pojo.MutiController;
import com.qilin.hscs.hm.util.MqttResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 10188 on 2018/4/25.
 */
@Slf4j
@Service("MutiController")
public class MutiControllerServiceImpl extends ServiceCommon implements MutiControllerService {

    private JSONObject msgObj;

    @Autowired
    private MuticontrollerRepository repository;

    @Override
    public void findTotalMutiController() {
        log.info("查询所有多控");
        String phone = dataObj.getString("phone");
        List<MutiController> mutiControllerList = this.repository.findAll();
        Map<String, Object> data = new HashMap<>();
        data.put("phone",phone);
        data.put("muticontroller",mutiControllerList);
        msg= MqttResultUtil.getResult(13001, 0, data);
    }

    @Override
    public void addMutiController() {
        log.info("添加多控");
        JSONObject mutiControllerObj = msgObj.getJSONObject("data");
        MuticontrollerDto muticontrollerDto = mutiControllerObj.toJavaObject(MuticontrollerDto.class);
        MutiController mutiController = DTO2DOConvert.MutiControllerDto2MutiController(muticontrollerDto);
        mutiController.setName(muticontrollerDto.getName());
        mutiController.setIcon(muticontrollerDto.getIcon());
        MutiController result = this.repository.save(mutiController);
        Map<String, Object> data = new HashMap<>();
        data.put("id",mutiController.getId());
        data.put("name",mutiController.getName());
        data.put("icon",mutiController.getIcon());
        data.put("gId",mutiController.getGld());
        msg= MqttResultUtil.getResult(13002, 0, data);
    }

    @Override
    public void alterMutiController() {
        log.info("修改多控");
        JSONObject mutiControllerObj = msgObj.getJSONObject("data");
        MuticontrollerDto muticontrollerDto = mutiControllerObj.toJavaObject(MuticontrollerDto.class);
        MutiController mutiController = DTO2DOConvert.MutiControllerDto2MutiController(muticontrollerDto);
        MutiController m = this.repository.findOne(muticontrollerDto.getId());
        if (mutiController.getId() != null) {
            mutiController.setName(muticontrollerDto.getName());
            mutiController.setIcon(muticontrollerDto.getIcon());
            MutiController result = this.repository.save(mutiController);
            Map<String, Object> data = new HashMap<>();
            data.put("phone", phone);
            data.put("id", mutiController.getId());
            data.put("name", mutiController.getName());
            data.put("icon", mutiController.getIcon());
            data.put("gId", mutiController.getGld());
            msg= MqttResultUtil.getResult(13003, 0, data);
        }else {
            log.error("【修改多控】修改状态不正确, Id={}", mutiController.getId());
            throw new CommonException(ResultEnum.MUTICONTROLLER_NOT_EXIST);
        }

    }

    @Override
    public void delMutiController() {
        log.info("删除多控");
        try {
            JSONObject dataObj = msgObj.getJSONObject("data");
            Integer id = dataObj.getInteger("mId");
            this.repository.delete(id);
            MutiController mutiController = new MutiController();
            Map<String, Object> data = new HashMap<>();
            data.put("phone", phone);
            Map<String, Object> muticontroller = new HashMap<>();
            muticontroller.put("id",id);
            data.put("muticontroller", muticontroller);
            msg= MqttResultUtil.getResult(13004, 0, data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    private MuticontrollerDto readDataToNuticontrollerDto() {

        JSONObject data = msgObj.getJSONObject("data");
        MuticontrollerDto muticontrollerDto = data.toJavaObject(MuticontrollerDto.class);
        return muticontrollerDto;
    }

    @Override
    public void handle() {

        switch (cmd) {
            case 12001:
                try {
                    findTotalMutiController();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                isflushToGateway(false);
                flush();
                break;
            case 12002:
                try {
                    addMutiController();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                isflushToGateway(false);
                flush();
                break;
            case 12003:
                try {
                    alterMutiController();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                isflushToGateway(false);
                flush();
                break;
            case 12004:
                try {
                    delMutiController();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                isflushToGateway(false);
                flush();
                break;
            default:

        }
    }
    @Override
    public void initailData(){
        try {
            msgObj = JSON.parseObject(msg);
            dataObj = msgObj.getJSONObject("data");
        } catch (JSONException je) {
            log.warn("传输过来的json字符串有误，主机无法解析 错误详细情况为={}", je.getStackTrace());
        }
        cmd = msgObj.getInteger("cmd");
        if (topicVar != null) {
            if (!topicVar.contains("/rsp")) {
                phone = msgObj.getString("phone");
            }
        }
    }
}
